using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace CodePlex.SharePoint.NoSPD
{
    class NoSPDHttpHandlerReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            SPWebConfigModification mod = new SPWebConfigModification("add[@type='CodePlex.SharePoint.NoSPD.NoSPDHttpHandler, CodePlex.SharePoint.NoSPD, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8c78433666f8c120']", "configuration/system.web/httpHandlers");
            mod.Owner = properties.Feature.DefinitionId.ToString();
            mod.Sequence = 0;
            mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            mod.Value = @"<add verb=""*"" path=""*/_vti_bin/_vti_aut/author.dll"" type=""CodePlex.SharePoint.NoSPD.NoSPDHttpHandler, CodePlex.SharePoint.NoSPD, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8c78433666f8c120"" />";
            webApp.WebConfigModifications.Add(mod);

            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            webApp.Update();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            foreach (SPWebConfigModification mod in webApp.WebConfigModifications)
            {
                if (mod.Owner == properties.Feature.DefinitionId.ToString())
                {
                    webApp.WebConfigModifications.Remove(mod);
                    break;
                }
            }
            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            webApp.Update();
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }
    }
}
